package org.example.ui.milk_order;

import org.example.business.AddToCartBusiness;
import org.example.model.MilkTea;
import org.example.model.Order;
import org.example.model.OrderItem;
import org.example.ui.customer.BottomPanel;
import org.example.ui.customer.DispositionSettleDialog;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.*;
import java.util.List;

public class MilkTeaItemPanel extends JPanel {
    private SpringLayout layout;
    private JLabel milkTeaLabel, priceLabel,
            conditionLabel, dateLabel;
    public JLabel numberLabel;
    private JButton addButton, moreButton, subtractButton;
    private Image image;
    public MilkTea milkTea = new MilkTea();
    public OrderItem orderItem = new OrderItem();
    public Order order = new Order();
    private JPanel panel;
    public MilkTeaItemPanel(MilkTea milkTea, int location) {
        this.milkTea.milkName = milkTea.milkName;
        this.milkTea.imagePath = milkTea.imagePath;
        this.milkTea.price = milkTea.price;
        this.milkTea.inventory = milkTea.inventory;
        setThisPanel();
        setMilk(milkTea);
        setShopButton();
        addButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (milkTea.inventory != 0) {
                    new DispositionSettleDialog(milkTea).setVisible(true);
                }
                else JOptionPane.showMessageDialog(MilkTeaItemPanel.this, "该奶茶已售空");
            }
        });
    }
    public MilkTeaItemPanel(Order order) {
        layout = new SpringLayout();
        setBackground(Color.white);
        setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
        setLayout(layout);
        setOrder(order);
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                new DispositionSettleDialog(order, 1).setVisible(true);
            }
            @Override
            public void mouseExited(MouseEvent e) {
                setBackground(Color.white);
                panel.setBackground(Color.white);
            }
        });
        layout = new SpringLayout();
        setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
        addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                setBackground(Color.LIGHT_GRAY);
                panel.setBackground(Color.LIGHT_GRAY);
            }
        });
    }
    public MilkTeaItemPanel(OrderItem orderItem) {
        this.orderItem = orderItem;
        setThisPanel();
        setMilk(milkTea);
        setShopButton();
        setShop(orderItem.milkName, orderItem.specification, orderItem.price);
        numberLabel.setText("" + orderItem.number);
        subtractButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(numberLabel.getText().equals("1")) {
                    AddToCartBusiness.itemMap.get(orderItem.milkName).setNumberLabel(-1);
                    AddToCartBusiness.cart.removeOrderItem(orderItem);
                    int num = AddToCartBusiness.map.get(orderItem.milkName) - 1;
                    AddToCartBusiness.map.put(orderItem.milkName, num);
                    AddToCartBusiness.setOrderItems();
                    return;
                }
                AddToCartBusiness.itemMap.get(orderItem.milkName).setNumberLabel(-1);
                AddToCartBusiness.map.put(orderItem.milkName, AddToCartBusiness.map.get(orderItem.milkName) - 1);
                AddToCartBusiness.cart.modifyOrderItem(orderItem, -1);
                AddToCartBusiness.setOrderItems();
            }
        });
        addButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int num = AddToCartBusiness.map.get(orderItem.milkName) + 1;
                AddToCartBusiness.itemMap.get(orderItem.milkName).setNumberLabel(1);
                AddToCartBusiness.map.put(orderItem.milkName, num);
                AddToCartBusiness.cart.modifyOrderItem(orderItem, 1);
                AddToCartBusiness.setOrderItems();
            }
        });
    }
    private void setThisPanel(){
        layout = new SpringLayout();
        setBackground(Color.white);
        setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
        addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                if (milkTea.inventory != 0)
                    setBackground(Color.LIGHT_GRAY);
            }
        });
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {
                if (milkTea.inventory != 0)
                    setBackground(Color.white);
            }
        });
    }
    public void setMilk(MilkTea milkTea) {
        milkTeaLabel = new JLabel(milkTea.milkName);
        priceLabel = new JLabel("￥" + milkTea.price + "起");
        conditionLabel = new JLabel();
        image = new ImageIcon(milkTea.imagePath).getImage().
                getScaledInstance(100,100,Image.SCALE_DEFAULT);
        milkTeaLabel.setIcon(new ImageIcon(image));
        setLayout(layout);
        add(milkTeaLabel);
        add(priceLabel);
        milkTeaLabel.setFont(new Font("华文行楷",Font.BOLD,28));
        priceLabel.setFont(new Font("草书",Font.ITALIC,15));
        priceLabel.setForeground(Color.RED);
        layout.putConstraint(SpringLayout.NORTH, milkTeaLabel,0,SpringLayout.NORTH,this);
        layout.putConstraint(SpringLayout.WEST,milkTeaLabel,0,SpringLayout.WEST,this);
        layout.putConstraint(SpringLayout.NORTH,priceLabel,15,SpringLayout.SOUTH,milkTeaLabel);
        layout.putConstraint(SpringLayout.WEST,priceLabel,0,SpringLayout.WEST,this);
        layout.putConstraint(SpringLayout.SOUTH,priceLabel,40,SpringLayout.SOUTH,milkTeaLabel);
    }
    public void setShopButton() {
        addButton = new JButton("+");
        subtractButton = new JButton("-");
        numberLabel = new JLabel("0");
        addButton.setFont(new Font("",Font.BOLD,25));
        addButton.setContentAreaFilled(false);
        addButton.setBorderPainted(false);
        numberLabel.setFont(new Font("",Font.BOLD,25));
        subtractButton.setBorderPainted(false);
        subtractButton.setFont(new Font("",Font.BOLD,25));
        subtractButton.setContentAreaFilled(false);
        addButton.setForeground(Color.red);
        add(addButton);
        add(numberLabel);
        add(subtractButton);
        layout.putConstraint(SpringLayout.NORTH,addButton,0,SpringLayout.NORTH,this);
        layout.putConstraint(SpringLayout.EAST,addButton,0,SpringLayout.EAST,this);
        layout.putConstraint(SpringLayout.NORTH,numberLabel,5,SpringLayout.NORTH,this);
        layout.putConstraint(SpringLayout.EAST,numberLabel,0,SpringLayout.WEST,addButton);
        layout.putConstraint(SpringLayout.NORTH,subtractButton,0,SpringLayout.NORTH,this);
        layout.putConstraint(SpringLayout.EAST,subtractButton,0,SpringLayout.WEST,numberLabel);
    }
    public void setOrder(Order order) {
        conditionLabel = new JLabel(order.condition);
        panel = new JPanel();
        SpringLayout springLayout = new SpringLayout();
        panel.setLayout(springLayout);
        JScrollPane scrollPane = new JScrollPane(panel);
        List<String> list = new ArrayList<>();
        Set<String> set = new HashSet<>();
        for (int i = 0; i < order.orderItems.size(); i++) {
            if(!set.contains(order.orderItems.get(i).imagePath)) {
                set.add(order.orderItems.get(i).imagePath);
                list.add(order.orderItems.get(i).imagePath);
            }
        }
        panel.setPreferredSize(new Dimension(110 * list.size(), 100));
        JLabel[] labels = new JLabel[list.size()];
        labels[0] = new JLabel();
        JScrollBar scrollBar = scrollPane.getHorizontalScrollBar();
        scrollPane.setBackground(Color.white);
        panel.setBackground(Color.white);
        scrollPane.setBorder(null);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollBar.setUnitIncrement(20);
        scrollBar.setOpaque(false);
        scrollBar.setPreferredSize(new Dimension(20,0));
        add(scrollPane);
        layout.putConstraint(SpringLayout.NORTH, scrollPane,0, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.SOUTH, scrollPane,-25, SpringLayout.SOUTH, this);
        layout.putConstraint(SpringLayout.EAST, scrollPane, 220, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.WEST, scrollPane, 0, SpringLayout.WEST, this);
        panel.add(labels[0]);
        Image image = new ImageIcon(list.getFirst()).getImage().
                getScaledInstance(100,100,Image.SCALE_DEFAULT);
        labels[0].setIcon(new ImageIcon(image));
        springLayout.putConstraint(SpringLayout.NORTH, labels[0], 0, SpringLayout.NORTH, panel);
        springLayout.putConstraint(SpringLayout.NORTH, labels[0], 0, SpringLayout.NORTH, panel);
        for (int i = 1; i < list.size(); i++) {
            labels[i] = new JLabel();
            Image imag = new ImageIcon(list.get(i)).getImage().
                    getScaledInstance(100,100,Image.SCALE_DEFAULT);
            labels[i].setIcon(new ImageIcon(imag));
            panel.add(labels[i]);
            springLayout.putConstraint(SpringLayout.NORTH, labels[i],0,SpringLayout.NORTH,panel);
            springLayout.putConstraint(SpringLayout.WEST, labels[i],10,SpringLayout.EAST,labels[i-1]);
        }
        moreButton = new JButton("再来一单");
        priceLabel = new JLabel("共" + order.total + "件" + "￥" + order.totalPrice);
        dateLabel = new JLabel(order.date);
        conditionLabel.setFont(new Font("",Font.BOLD,15));
        conditionLabel.setForeground(Color.GRAY);
        moreButton.setContentAreaFilled(false);
        moreButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                BottomPanel.moreOrder();
            }
        });
        add(conditionLabel);
        add(moreButton);
        add(priceLabel);
        add(dateLabel);
        layout.putConstraint(SpringLayout.NORTH,conditionLabel,0,SpringLayout.NORTH,this);
        layout.putConstraint(SpringLayout.EAST,conditionLabel,0,SpringLayout.EAST,this);
        layout.putConstraint(SpringLayout.EAST,moreButton,0,SpringLayout.EAST,this);
        layout.putConstraint(SpringLayout.SOUTH,moreButton,-5,SpringLayout.SOUTH,this);
        layout.putConstraint(SpringLayout.EAST,priceLabel,0,SpringLayout.EAST,this);
        layout.putConstraint(SpringLayout.SOUTH,priceLabel,-5,SpringLayout.NORTH,moreButton);
        layout.putConstraint(SpringLayout.NORTH,dateLabel,10,SpringLayout.SOUTH,scrollPane);
        layout.putConstraint(SpringLayout.WEST,dateLabel,5,SpringLayout.WEST,this);
    }
    public void setShop(String name, String disposition, double price) {
        Image image1 = new ImageIcon(orderItem.imagePath).getImage().
                getScaledInstance(50,50,Image.SCALE_DEFAULT);
        milkTeaLabel.setIcon(new ImageIcon(image1));
        addButton.setFont(new Font("",Font.BOLD,15));
        numberLabel.setFont(new Font("",Font.BOLD,15));
        subtractButton.setFont(new Font("",Font.BOLD,15));
        milkTeaLabel.setText("");
        JLabel nameLabel = new JLabel("<html>"+name+"<br/>"+"￥"+price+"<html/>"),
                dispositionLabel = new JLabel(disposition);
        remove(priceLabel);
        add(nameLabel);
        add(dispositionLabel);
        layout.putConstraint(SpringLayout.WEST,nameLabel,5,SpringLayout.EAST,milkTeaLabel);
        layout.putConstraint(SpringLayout.NORTH,nameLabel,0,SpringLayout.NORTH,this);
        layout.putConstraint(SpringLayout.NORTH,dispositionLabel,0,SpringLayout.SOUTH,milkTeaLabel);
        layout.putConstraint(SpringLayout.WEST,dispositionLabel,0,SpringLayout.WEST,this);
    }
    public int setNumberLabel(int n) {
        String str = numberLabel.getText();
        int num = Integer.parseInt(str);
        num += n;
        numberLabel.setText(String.format("%d", num));
        return num;
    }
}
